package com.leetcode.June;

import java.util.WeakHashMap;

/**
 * @Description: 环形链表
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-11 10:54
 */
public class hasCycle {
    //    public boolean hasCycle(ListNode head) {
//        if (head == null) return false;
//        ListNode slow = head;
//        ListNode fast = head;
//        while (fast != null && fast.next != null) {
//            slow = slow.next;
//            fast = fast.next.next;
//            if (slow == fast) return true;
//        }
//        return false;
//    }
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                while (slow != head) {
                    slow = slow.next;
                    head=head.next;
                }
                return slow;
            }
        }
        return null;
    }
}

